package com.senyint.hip.fhir.provider;

import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.server.IResourceProvider;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.utilities.xhtml.NodeType;
import org.hl7.fhir.utilities.xhtml.XhtmlNode;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * *******description*******
 * <p>
 * *************************
 *
 * @author ChenPan
 * @date 2019/7/2 16:40
 */
@Component
public class ValueSetProvider implements IResourceProvider {

    private Map<String, ValueSet> ValueSets = new HashMap<String, ValueSet>();

    @Override
    public Class<ValueSet> getResourceType() {
        return ValueSet.class;
    }

    @PostConstruct
    public void init() {
        ValueSet valueSet = new ValueSet();
        valueSet.setId(new IdDt("ValueSet","id321655"));;
        Narrative narrative = new Narrative();
        narrative.setDiv(new XhtmlNode(NodeType.Element)
                .addText("婚姻状况代码值集。"));

        valueSet.setUrl("http://hl7china.org.cn/fhir/ValueSet/maritalstatus");
        valueSet.setName("婚姻状况代码值集");
        valueSet.setTitle("婚姻状况代码值集");
        valueSet.setStatus(Enumerations.PublicationStatus.ACTIVE);

        ValueSet.ConceptSetComponent component = new ValueSet.ConceptSetComponent();
        component.setSystem("2.16.156.10011.2.3.3.5");
        component.addConcept().setCode("10").setDisplay("未婚");
        component.addConcept().setCode("20").setDisplay("已婚");
        component.addConcept().setCode("21").setDisplay("初婚");
        component.addConcept().setCode("22").setDisplay("再婚");
        component.addConcept().setCode("23").setDisplay("复婚");
        component.addConcept().setCode("30").setDisplay("丧婚");
        component.addConcept().setCode("40").setDisplay("离婚");
        component.addConcept().setCode("90").setDisplay("未说明的婚姻状况");
        valueSet.setCompose(new ValueSet.ValueSetComposeComponent().addInclude(component));

        ValueSets.put("id321655", valueSet);

        ValueSet valueSet2 = new ValueSet();
        valueSet2.setId(new IdDt("ValueSet","id321654"));
        Narrative narrative2 = new Narrative();
        narrative2.setDiv(new XhtmlNode(NodeType.Element)
                .addText("婚姻状况代码表中表示已婚的代码集。"));

        valueSet2.setUrl("http://hl7china.org.cn/fhir/ValueSet/maritalstatus-married");
        valueSet2.setName("表示已婚的代码集");
        valueSet2.setTitle("婚姻状况代码表中表示已婚的代码集。");
        valueSet2.setStatus(Enumerations.PublicationStatus.ACTIVE);

        ValueSet.ConceptSetComponent component2 = new ValueSet.ConceptSetComponent();
        component2.setSystem("2.16.156.10011.2.3.3.5");
        component2.addConcept().setCode("20").setDisplay("已婚");
        component2.addConcept().setCode("21").setDisplay("初婚");
        component2.addConcept().setCode("22").setDisplay("再婚");
        component2.addConcept().setCode("23").setDisplay("复婚");
        valueSet2.setCompose(new ValueSet.ValueSetComposeComponent().addInclude(component2));

        ValueSets.put("id321654", valueSet);
    }

    @Read
    public ValueSet findValueSet(@IdParam IdType theId) {

        return ValueSets.get(theId.getIdPart());

    }

}
